<template>
	<view class="content b-t">
		<u-swipe-action>
			<u-swipe-action-item :options="options1" v-for="(item, index) in addressList" :key="index"
				@click="deleteAddress(item)">
				<view class="list b-b" @click="checkAddress(item)">
					<view class="wrapper">
						<view class="address-box">
							<view class="address-box-div" v-if="item.defaultAddress == 1">
								<u-tag text="默认" plain size="mini" type="warning"></u-tag>
							</view>
							<view class="address-box-div">
								<u-tag :text="item.label" plain size="mini"></u-tag>
							</view>
							<view class="address">
								<text class="">{{item.address}} {{item.houseNumber}}</text>
							</view>

						</view>
						<view class="u-box">
							<text class="name">{{item.name}} {{item.gender==0?'先生':'女士'}}</text>
							<text class="mobile">{{item.phone}}</text>
						</view>
					</view>
					<text class="yticon icon-bianji" @click.stop="addAddress('edit', item)"></text>
				</view>
			</u-swipe-action-item>
		</u-swipe-action>

		<button class="add-btn" @click="addAddress('add')">新增地址</button>
	</view>
</template>

<script>
	import {
		addressAdministrationList,
		addressAdministrationDelete
	} from '@/api/address.js'
	export default {
		data() {
			return {
				options1: [{
					text: '删除',
					style: {
						backgroundColor: 'red'
					}
				}],
				source: 0,
				addressList: [{
					name: '刘晓晓',
					mobile: '18666666666',
					addressName: '贵族皇仕牛排(东城店)',
					address: '北京市东城区',
					area: 'B区',
					default: true
				}, {
					name: '刘大大',
					mobile: '18667766666',
					addressName: '龙回1区12号楼',
					address: '山东省济南市历城区',
					area: '西单元302',
					default: false,
				}]
			}
		},
		onLoad(option) {
			this.source = option.source;
		},
		onShow() {
			this.getList()
		},
		methods: {
			// 删除
			async deleteAddress(item) {
				let res = await addressAdministrationDelete(item.id)
				if (res.code == 200) {
					this.getList()
					console.log(res, 99);
				}
			},
			//获取列表
			async getList() {
				let res = await addressAdministrationList()
				if (res.code == 200) {
					this.addressList = res.data
				}
			},
			//选择地址
			checkAddress(item) {
				if (this.source == 1) {
					this.$api.prePage().addressData = item;
					uni.navigateBack()
				}
			},
			// 新增或修改地址
			addAddress(type, item) {
				uni.navigateTo({
					url: `/pages/address/addressManage?type=${type}&data=${JSON.stringify(item)}`
				})
			},
		}
	}
</script>

<style lang='scss'>
	page {
		padding-bottom: 120upx;
	}

	.content {
		position: relative;
	}

	.list {
		display: flex;
		align-items: center;
		padding: 20upx 30upx;
		;
		background: #fff;
		position: relative;
	}

	.wrapper {
		display: flex;
		flex-direction: column;
		width: 90%;
	}

	.address-box {
		display: flex;
		align-items: center;
		width: 100%;
		gap: 10rpx;

		.address-box-div {
			min-width: 50rpx;
		}

		.address {
			width: 390rpx;
			word-break: break-all;
			font-size: 30upx;
			color: $font-color-dark;
		}
	}

	.u-box {
		font-size: 28upx;
		color: $font-color-light;
		margin-top: 16upx;

		.name {
			margin-right: 30upx;
		}
	}

	.icon-bianji {
		display: flex;
		align-items: center;
		height: 80upx;
		font-size: 40upx;
		color: $font-color-light;
		padding-left: 30upx;
	}

	.add-btn {
		position: fixed;
		left: 30upx;
		right: 30upx;
		bottom: 16upx;
		z-index: 95;
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690upx;
		height: 80upx;
		font-size: 32upx;
		color: #fff;
		background-color: $base-color;
		border-radius: 10upx;
		box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
	}
</style>